/**
 * 栏位配置相关
 */
 import {
  getReportColInfo, //获取栏位配置接口
  getReportColAdd, //保存栏位配置接口新增
  getReportColEdit, //保存栏位配置接口修改
  getReportColDelete, // 栏位配置接口删除
} from '@/api/websheet.js'

export const LwMixin = {
  data() {
    return {
      vId: 1,
      headList: [], //本地栏位列表
      headListNosave: [], //已保存栏位列表
    }
  },
  created() {
  },
  methods: {
    // 获取栏位配置
    queryReportColInfo(item) {
      getReportColInfo({ bbdm: item.bbdm }).then((res) => {
        if (res.code === 200) {
          let list = res.result.records || []
          list.map((item) => {
            if (item.lwbm) {
              item.lwbm = JSON.parse(item.lwbm)
            }
          })
          this.headList = list
        } else {
          this.$message.warning('服务器出错!')
        }
      })
    },
    // 保存栏位配置
    saveHead(record) {
      if (record.id) {
        getReportColEdit({ ...record, lwbm: JSON.stringify(record.lwbm) }).then((res) => {
          if (res.code === 200) {
            this.$message.success('修改栏位配置成功')
            this.queryReportColInfo(record)
          } else {
            this.$message.warning('服务器出错!')
          }
        })
      } else {
        getReportColAdd({ ...record, lwbm: JSON.stringify(record.lwbm) }).then((res) => {
          if (res.code === 200) {
            this.$message.success('保存栏位配置成功')
            let list = this.headListNosave.filter(
              (item) => !(item.lwbm.row == record.lwbm.row && item.lwbm.column == record.lwbm.column)
            )
            this.headListNosave = list
            this.queryReportColInfo(record)
          } else {
            this.$message.warning('服务器出错!')
          }
        })
      }
    },
    // 栏位配置删除
    deleteLwpz(record) {
      if (!Object.hasOwn(record, 'id')) {
        let list = this.headListNosave.filter(
          (item) => !(item.lwbm.row == record.lwbm.row && item.lwbm.column == record.lwbm.column)
        )
        this.headListNosave = list
        this.$message.success('删除成功')
        return
      }
      getReportColDelete({ id: record.id }).then((res) => {
        if (res.code === 200) {
          this.$message.success('删除成功')
          this.queryReportColInfo(record)
        } else {
          this.$message.warning('服务器出错!')
        }
      })
    },
  }
}